//
//  LoginViewController.m
//  HeWeiZhiNeng
//
//  Created by 林建 on 17/12/2.
//  Copyright © 2017年 YanCeyMichael.github.io. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "LoginView.h"
@interface LoginViewController ()<UIScrollViewDelegate,LoginViewDelegate>
@property (nonatomic, strong)LoginView *loginView;
@property (nonatomic, assign)BOOL isQuickType;
@property (nonatomic, assign)NSInteger timeNumber;
@property (nonatomic, strong)NSTimer *timer;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.isQuickType = YES;
    self.timeNumber = 71;
    [self.view addSubview:self.loginView];
    [self.loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self selectLoginType:self.isQuickType];
    UITapGestureRecognizer *timeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(timeAction)];
    [self.loginView.timeLabel addGestureRecognizer:timeTap];
    
    UIView *back = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    [self.view addSubview:back];
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backAction)];
    [back addGestureRecognizer:tapGr];
    // Do any additional setup after loading the view.
}

-(void)backAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark ----- 创建登录视图 -----
-(LoginView *)loginView{
    if (!_loginView) {
        _loginView = [[LoginView alloc]init];
        _loginView.scrollView.delegate = self;
        _loginView.delegate = self;
    }
    return _loginView;
}

#pragma mark - UIScrollView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (round(scrollView.contentOffset.x / kScreenW) > 0.5) {
        self.isQuickType = NO;
    } else {
        self.isQuickType = YES;
    }
    [self selectLoginType:self.isQuickType];
}

-(void)selectLoginType:(BOOL)type{
    self.loginView.quickLoginLabel.textColor   = type ? Color.selected_blue : Color.fullBtnTitle;
    self.loginView.accountLoginLabel.textColor = type ? Color.fullBtnTitle : Color.selected_blue;
}

#pragma mark ----- 获取验证码 -----
-(void)timeAction{
    if (![self.loginView.timeLabel.text containsString:@"重发"]) {
        [self timer];
    }
}

#pragma mark - 计时
#pragma mark ----- 计时器 -----
-(NSTimer *)timer{
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(time) userInfo:nil repeats:YES];
    
    return _timer;
}

#pragma mark 倒计时
-(void)time{
    self.timeNumber--;
    if (self.timeNumber <= 0) {
        [_timer invalidate];
//        Login_Phone = !Login_Phone;
        self.loginView.timeLabel.text = @"重新发送";
        self.timeNumber = 61;
        return;
    }
    self.loginView.timeLabel.text = [NSString stringWithFormat:@"重发（%ld）",_timeNumber];
    
}

#pragma mark ----- 头视图协议方法 -----
- (void)userChoose:(NSInteger)tag {
    switch (tag) {
        case 9:
            [self GotoRegistView];
            break;
            
        default:
            break;
    }
}

#pragma mark ----- 跳转登录 -----
-(void)GotoRegistView{
    RegistViewController *registVC = [[RegistViewController alloc]init];
    [self.navigationController pushViewController:registVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
